package com.crm.controller.saling;

import com.crm.pojo.Refund;
import com.crm.service.saling.RefundService;
import com.crm.vo.RefundContractClient;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dr5hx on 2018/8/31
 */
@Controller
public class RefundController {
    @Autowired
    private RefundService refundService;

    /**
     * 显示退货数据
     *
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("getRefundContractClientList")
    @ResponseBody
    public Map getRefundContractClientList(int page, int rows) {
        try {
            List<RefundContractClient> list = refundService.getRefundContractClientList(page, rows);
            Long count = refundService.getRefundCount();
            Map map = new HashMap();
            map.put("rows", list);
            map.put("total", count);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 转发到退款单
     *
     * @return
     */
    @GetMapping("toRefundContractClientList")
    public String toRefundContractClientList() {
        return "saling/refundList";


    }

    @PostMapping("completeRefundOperate")
    @ResponseBody
    public Result completeRefundOperate(Long refundId) {
        try {
            Refund r = refundService.findOneById(refundId);
            if (!r.getRefundState().equals("完成")) {
                r.setRefundState("完成");
                r.setRefundAlreadyMoney(r.getRefundMoney());
                refundService.saveARefund(r);
                return Result.SUCCESS;
            } else {
                return new Result("已经被修改");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("操作失败");
        }
    }

}
